package com.salt.system.handlers;

import com.salt.system.enums.SysUserState;
import org.apache.ibatis.type.EnumTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Objects;

/**
 * 系统用户状态处理器
 *
 * @author HaiBo Kuang
 * @date 2021年04月20日 下午 17:49
 */
@MappedJdbcTypes(JdbcType.INTEGER)
public class SysUserStateHandler extends EnumTypeHandler<SysUserState> {
    public SysUserStateHandler(Class<SysUserState> type) {
        super(type);
    }

    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, SysUserState parameter, JdbcType jdbcType) throws SQLException {
        ps.setInt(i, parameter.getState());
    }

    @Override
    public SysUserState getNullableResult(ResultSet rs, String columnName) throws SQLException {
        int ret = rs.getInt(columnName);
        SysUserState[] values = SysUserState.values();
        for (SysUserState value : values) {
            if (Objects.equals(ret, value.getState())) {
                return value;
            }
        }
        return SysUserState.NONE;
    }
}
